home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / ImageTest / ImagePanel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  1.8 KB  |  43 lines

  1. import java.applet.Applet;
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.GridLayout;
  6. import java.awt.Image;
  7. import java.awt.Panel;
  8. import java.awt.image.MemoryImageSource;
  9.  
  10. class ImagePanel extends Panel {
  11.    Applet applet;
  12.  
  13.    public ImagePanel(Applet app) {
  14.       this.applet = app;
  15.       ((Container)this).setLayout(new BorderLayout());
  16.       Panel grid = new Panel();
  17.       ((Container)grid).setLayout(new GridLayout(0, 2));
  18.       ((Container)this).add("Center", grid);
  19.       ((Container)grid).add(new ImageCanvas(this.applet, this.makeDitherImage(), (double)0.5F));
  20.       Image joe = this.applet.getImage(this.applet.getDocumentBase(), "graphics/joe.surf.yellow.small.gif");
  21.       ((Container)grid).add(new ImageCanvas(this.applet, joe, (double)1.0F));
  22.       ((Component)this).reshape(0, 0, 20, 20);
  23.    }
  24.  
  25.    Image makeDitherImage() {
  26.       int w = 100;
  27.       int h = 100;
  28.       int[] pix = new int[w * h];
  29.       int index = 0;
  30.  
  31.       for(int y = 0; y < h; ++y) {
  32.          int red = y * 255 / (h - 1);
  33.  
  34.          for(int x = 0; x < w; ++x) {
  35.             int blue = x * 255 / (w - 1);
  36.             pix[index++] = -16777216 | red << 16 | blue;
  37.          }
  38.       }
  39.  
  40.       return this.applet.createImage(new MemoryImageSource(w, h, pix, 0, w));
  41.    }
  42. }
  43.